package api.email;

import java.io.*;
import java.util.ArrayList;

public class SetEmailContent {

    public void setEmailContent(ArrayList<EmailContentBean> list, File errefilePath) {

        if (list == null) {
            System.out.println("list == null");
            return;
        }
        if (list.size() <= 0) {
            System.out.println("list.size() <= 0");
            return;
        }
        ArrayList<EmailContentBean> erreList2 = new ArrayList<>();
        try {
            String apiNmaeTxt = FileInputStream(errefilePath).replaceAll("\u0000", "").replace("\n", "");
            if (!apiNmaeTxt.equals("")) {
                //上一次调用错误的接口名       nameList
                //与这次调用错误的接口名对比    list
                //如果这个接口连续两次出错,就添加到邮件中
                String[] nameList = apiNmaeTxt.split(",");
                for (String s : nameList) {
                    for (EmailContentBean e : list) {
                        if (e.getApiName().equals(s)) {
                            erreList2.add(e);
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取上次错误接口名文件错误");
        }

        String apiNmaeList = "";
        for (int i = 0, k = list.size(); i < k; i++) {
            EmailContentBean e = list.get(i);
            if (i == k - 1) {
                apiNmaeList += e.getApiName();
            } else {
                apiNmaeList += e.getApiName() + ",";
            }
        }
        if (!apiNmaeList.equals("")) {
            try {
                //写入这次报错的接口名
                PrintStreamDemo(errefilePath, apiNmaeList);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("错误接口名写入错误>> \n" + e.getMessage());
            }
        }
        String content = "";
        String title = "";
        if (erreList2.size() > 0) {
            SendEmail sendEmail = new SendEmail();
            content = sendEmail.createEmailContent(erreList2);
            title = title + "共:( " + erreList2.size() + " )个接口";
            list.clear();
            erreList2.clear();
            sendEmail.sendmessage(
                    new String[]{"yang.liang@vpclub.cn"}
                    , title, content);
        }
    }

    //                            "liu.hua@vpclub.cn"
//                            "yang.jiaqi@vpclub.cn," +
//                            "zhou.hui@vpclub.cn," +
//                            "jiang.jianfu@vpclub.cn," +
//                            "liu.junyuan@vpclub.cn"
    //写入内容
    public void PrintStreamDemo(File file, String apiNmaeList) throws FileNotFoundException {
        FileOutputStream out = new FileOutputStream(file);
        PrintStream p = new PrintStream(out);
        p.println(apiNmaeList);
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("PrintStreamDemo:" + e.getMessage());
        }
    }

    //读取文件内容
    public String FileInputStream(File file) throws IOException {
        if (!file.exists() || file.isDirectory())
            throw new FileNotFoundException();
        FileInputStream fis = new FileInputStream(file);
        byte[] buf = new byte[1024];
        StringBuffer sb = new StringBuffer();
        while ((fis.read(buf)) != -1) {
            sb.append(new String(buf));
            buf = new byte[1024];//重新生成，避免和上次读取的数据重复
        }
        fis.close();
        return sb.toString();
    }

}
